﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Data.SQLite; 
namespace Bussiness
{
    /// <summary>
    /// 类TemFields。
    /// </summary>
    [Serializable]
    public partial class TemFields
    {
        public TemFields()
        { }
        #region Model
        private int _id;
        private string _fields;
        private string _desc;
        private string _fieldsname;
        /// <summary>
        /// 
        /// </summary>
        public int id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string fields
        {
            set { _fields = value; }
            get { return _fields; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string desc
        {
            set { _desc = value; }
            get { return _desc; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string fieldsName
        {
            set { _fieldsname = value; }
            get { return _fieldsname; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TemFields(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,fields,desc,fieldsName ");
            strSql.Append(" FROM [TemFields] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id",  DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fields"] != null && ds.Tables[0].Rows[0]["fields"].ToString() != "")
                {
                    this.fields = ds.Tables[0].Rows[0]["fields"].ToString();
                }
                if (ds.Tables[0].Rows[0]["desc"] != null && ds.Tables[0].Rows[0]["desc"].ToString() != "")
                {
                    this.desc = ds.Tables[0].Rows[0]["desc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fieldsName"] != null && ds.Tables[0].Rows[0]["fieldsName"].ToString() != "")
                {
                    this.fieldsName = ds.Tables[0].Rows[0]["fieldsName"].ToString();
                }
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [TemFields]");
            strSql.Append(" where id=@id ");

            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id",  DbType.Int32,4)};
            parameters[0].Value = id;

            return SqliteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [TemFields] (");
            strSql.Append("fields,desc,fieldsName)");
            strSql.Append(" values (");
            strSql.Append("@fields,@desc,@fieldsName)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@fields", DbType.String),
					new SQLiteParameter("@desc", DbType.String),
					new SQLiteParameter("@fieldsName", DbType.String)};
            parameters[0].Value = fields;
            parameters[1].Value = desc;
            parameters[2].Value = fieldsName;

            SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [TemFields] set ");
            strSql.Append("fields=@fields,");
            strSql.Append("desc=@desc,");
            strSql.Append("fieldsName=@fieldsName");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@fields", DbType.String),
					new SQLiteParameter("@desc", DbType.String),
					new SQLiteParameter("@fieldsName", DbType.String),
					new SQLiteParameter("@id", DbType.Int32,8)};
            parameters[0].Value = fields;
            parameters[1].Value = desc;
            parameters[2].Value = fieldsName;
            parameters[3].Value = id;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [TemFields] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,fields,desc,fieldsName ");
            strSql.Append(" FROM [TemFields] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fields"] != null && ds.Tables[0].Rows[0]["fields"].ToString() != "")
                {
                    this.fields = ds.Tables[0].Rows[0]["fields"].ToString();
                }
                if (ds.Tables[0].Rows[0]["desc"] != null && ds.Tables[0].Rows[0]["desc"].ToString() != "")
                {
                    this.desc = ds.Tables[0].Rows[0]["desc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fieldsName"] != null && ds.Tables[0].Rows[0]["fieldsName"].ToString() != "")
                {
                    this.fieldsName = ds.Tables[0].Rows[0]["fieldsName"].ToString();
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [TemFields] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqliteHelper.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

